import $ from "jquery";

/**
 *
 * @param {string | {content: string, duration?: number, color?: string}} options
 * @param {()=>void} [callback]
 */
export default function toast(options, callback) {
  const duration =
    typeof options == "object" && options.duration ? options.duration : 2000;
  const content = typeof options == "string" ? options : options.content;
  const toastTag = $(
    `<div class="ui-toast" style="opacity: 0">${content}</div>`
  );

  if (typeof options == "object" && options.color) {
    toastTag.css("color", options.color);
  }

  $(document.body).append(toastTag);
  setTimeout(() => {
    toastTag.css("opacity", 1);
  }, 0);

  setTimeout(() => {
    toastTag.on("transitionend", () => {
      toastTag.remove();
      if (callback) callback();
    });
    toastTag.css("opacity", 0);
  }, duration);
}
